package com.lemon.smartsafetynew.ui.adapter.hidden;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenStatusVo;

import java.util.List;

public class SafeHiddenDangerStatusAdapter extends RecyclerView.Adapter<SafeHiddenDangerStatusAdapter.Holder>
        implements View.OnClickListener {

    private Context mContext;
    private List<SafeHiddenStatusVo> mDatas;

    public SafeHiddenDangerStatusAdapter(Context context, List<SafeHiddenStatusVo> datas) {
        super();
        this.mContext = context;
        this.mDatas = datas;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    // 填充onCreateViewHolder方法返回的holder中的控件
    public void onBindViewHolder(Holder holder, int position) {

        if(mDatas.get(position).isSelected()) {
            holder.choose.setVisibility(View.VISIBLE);
            holder.textView.setSelected(true);
            holder.layout.setSelected(true);
        } else {
            holder.choose.setVisibility(View.GONE);
            holder.textView.setSelected(false);
            holder.layout.setSelected(false);
        }
        holder.layout.setTag(position);
        holder.textView.setText(mDatas.get(position).getStatusName());
    }

    @Override
    // 重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    public Holder onCreateViewHolder(ViewGroup parent, int arg1) {
        // 填充布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_hidden_danger_status_recyceler_item, parent, false);
        view.findViewById(R.id.layout_bg).setOnClickListener(this);
        return new Holder(view);
    }

    @Override
    public void onClick(View v) {
        if(mItemClickListener != null) {
            mItemClickListener.onItemClick((int)v.getTag());
        }
    }

    // 定义内部类继承ViewHolder
    class Holder extends RecyclerView.ViewHolder {

        private TextView textView;
        private ImageView choose;
        private RelativeLayout layout;

        Holder(View view) {
            super(view);
            textView = (TextView) view.findViewById(R.id.hidden_status_tv);
            choose = (ImageView) view.findViewById(R.id.selected_iv);
            layout = (RelativeLayout) view.findViewById(R.id.layout_bg);
        }

    }

    public void setItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    private OnItemClickListener mItemClickListener;
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}
